package org.gim.controller.news;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.gim.common.CommonResult;
import org.gim.entity.dto.news.NewsInfo;
import org.gim.service.news.INewsInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/news")
public class CommonNewsController {

    @Resource
    private INewsInfoService iNewsInfoService;

    @GetMapping("/getListTop10")
    public CommonResult<Map<String,List<String>>> getListTop10(){

        List<String> typeList = List.of("WANGYIXINWEN", "CSDN", "PENGPAI","XINLANGNEWS","DOUYIN","XINLANGB");

        Map<String,List<String>> map = new HashMap<>();

        for(String typeString : typeList){
            QueryWrapper<NewsInfo> qw = new QueryWrapper<>();
            qw.orderByDesc("publish_time");
            qw.eq("source_code",typeString);
            qw.last("LIMIT 10");
            List<NewsInfo> list = iNewsInfoService.list(qw);
            List<String> dataList = new ArrayList<>();

            for(NewsInfo itemData : list){
                dataList.add(itemData.getTitle());
            }
            map.put(typeString,dataList);
        }

        return CommonResult.successResponse(map);
    }
}
